package com.jcliprunner;

import ims.ImageContainer;

import org.gnome.gtk.Gtk;
import org.gnome.gtk.HBox;
import org.gnome.gtk.Image;
import org.gnome.gtk.Label;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.MenuItem.Activate;

/**
 * Homemade good looking menu
 * 
 * @author pengo
 * 
 */
public class TrayMenu extends Menu implements Activate {
	private final int MENU_ITEM_MAX_CHAR = 25;

	public TrayMenu() {
		addItem("python", "Run as <b>Python</b>", "python.png");
		addItem("java", "Run as <b>Java</b>", "java.png");
		addItem("php", "Run as <b>Php</b>", "php.png");
		addItem("ruby", "Run as <b>Ruby</b>", "ruby.png");
		addItem("cpp", "Run as <b>C++</b>", "cpp.png");
		
		
		
		addItem("exit", " <b>Exit</b> ", "exit.png");
		

	}

	public void addItem(String name, String markupText, String imageFile) {
		HBox box = new HBox(false, 0);
		MenuItem item = new MenuItem();
		item.connect(this);
		item.setName(name);

		Label label = new Label();
		label.setUseMarkup(true);
		label.setLabel(fixText(markupText));

		Image image = ImageContainer.getImage(imageFile);

		box.packStart(label, false, false, 0);
		box.packEnd(image, false, false, 0);
		item.add(box);
		append(item);
	}

	public String fixText(String text) {

		int i = text.length();

		// 25 max length of char
		i = Math.min(i, MENU_ITEM_MAX_CHAR);

		StringBuffer sb = new StringBuffer();
		sb.append(text.substring(0, i));

		// append spaces
		for (int ext = 0; ext < 25 - i; sb.append(' '), ext++)
			;

		// append a tab space for looking better
		sb.append('\t');

		// DEBUG : System.out.println(sb.toString()+".");

		return sb.toString();

	}

	public void onActivate(MenuItem source) {
		if (source.getName().equals("exit")) {
			Gtk.mainQuit();
		} else {
			// if not exit item, run code by language
			new Util(source.getName());
		}

	}

}
